{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7fe091f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "list_1 = [\"语文\",\"数学\",\"英语\"]\n",
    "print(len(list_1))# 获取长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "28da9f3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英语\n"
     ]
    }
   ],
   "source": [
    "print(list_1[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d2f4c235",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "物理\n",
      "['语文', '数学', '物理', '物理', '物理', '物理']\n"
     ]
    }
   ],
   "source": [
    "list_1.append(\"物理\")# 添加新的元素\n",
    "print(len(list_1))\n",
    "del list_1[-1]\n",
    "print(list_1[-1])\n",
    "print(list_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0b123975",
   "metadata": {},
   "outputs": [],
   "source": [
    "list_1.sort()# 默认升序\n",
    "list_1.sort(reverse=True)# 降序排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "55d75659",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "(50,)\n"
     ]
    }
   ],
   "source": [
    "#元组部分\n",
    "tuple_a = (50,)#只有一个元素的元组，需要加,\n",
    "print(type(tuple_a))\n",
    "print(tuple_a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5f03f09f",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_16380/2163059554.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mtuple_b\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"111123\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"小明\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;31m#元组的元素不能被修改；不能被删除，删除只能删除整个元组\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m# 元组的不可变指的是元组所指向的内存中的内容不可变。\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mtuple_b\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'H'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "tuple_b = (\"111123\", \"小明\")#元组的元素不能被修改；不能被删除，删除只能删除整个元组\n",
    "# 元组的不可变指的是元组所指向的内存中的内容不可变。\n",
    "tuple_b[0] = 'H'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "59b39c4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2411119978880\n",
      "2411116329856\n"
     ]
    }
   ],
   "source": [
    "print(id(tuple_b))\n",
    "tuple_b = (1,2,3)\n",
    "print(id(tuple_b))#内存地址发生了变化，重新赋值的元组 tup，绑定到新的对象了，不是修改了原来的对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3ff901ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不存在！\n",
      "{'小王', 1, 2, 3, '李华', '小明', '娜娜'}\n"
     ]
    }
   ],
   "source": [
    "#集合部分，元素不重复\n",
    "set_1 = {1,2,3,'小明','李华','小王'}\n",
    "set_1.add(\"娜娜\")\n",
    "if '小李' in set_1: #调用remove()之前，先in判断一下\n",
    "    set_1.remove('小李')\n",
    "    print(\"删除成功！\")\n",
    "else:\n",
    "    print(\"不存在！\")\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0c010503",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是一个好孩子！\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "#字典部分\n",
    "#键值对，key:value，对之间用,分割\n",
    "#值可以取任何数据类型，但键必须是不可变的，如字符串，数字。\n",
    "dict_student={\n",
    "              \"学号\":\"202109001\",\n",
    "              \"姓名\":\"小娜\",\n",
    "              \"年龄\":18, \n",
    "              \"性别\":\"female\", \n",
    "              \"课程\":[\"语文\",\"数学\", \"英语\"]\n",
    "            }\n",
    "if \"姓名\" in dict_student.keys(): # 判断key是否存在\n",
    "    if dict_student[\"姓名\"] == \"小娜\":\n",
    "        print(\"这是一个好孩子！\")\n",
    "        print(dict_student[\"年龄\"])\n",
    "else:\n",
    "    print(\"键不存在！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e9a695eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['语文', '数学', '英语']\n",
      "['语文', '数学', '英语', '物理']\n"
     ]
    }
   ],
   "source": [
    "if \"课程\" in dict_student.keys(): # 判断key是否存在\n",
    "    print(dict_student[\"课程\"])\n",
    "    if(isinstance(dict_student[\"课程\"], list)):\n",
    "        dict_student[\"课程\"].append(\"物理\")\n",
    "        print(dict_student[\"课程\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3dc09ef1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'学号': '202109001', '姓名': '小娜', '年龄': 18, '性别': 'female', '课程': ['语文', '数学', '英语', '物理'], '年级': '二年级'}\n"
     ]
    }
   ],
   "source": [
    "dict_student[\"年级\"] = \"二年级\"\n",
    "print(dict_student)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c2c13d35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'学号': '202109001', '姓名': '小娜', '年龄': 18, '课程': ['语文', '数学', '英语', '物理'], '年级': '二年级'}\n"
     ]
    }
   ],
   "source": [
    "if \"性别\" in dict_student.keys():\n",
    "    del dict_student[\"性别\"] #删除一个键值对\n",
    "    print(dict_student)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2e1ac7c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
